﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Data.Json;
using Windows.Devices.Geolocation;
using Xiaowei.Helpers;

namespace Xiaowei.Models
{
    public class MaintainPositionItem
    {
        public string Name { get; set; }
        public string Addr { get; set; }
        public double Latitude { get; set; }
        public double Longitude { get; set; }
        public int Distance { get; set; }
        public string DistanceString
        {
            get
            {
                return $"约{Distance}m";
            }
        }

    }
    public class MaintainPositionModel
    {
        public string Content { get; set; }
        public LoadingStateEnum LoadingState { get; set; } = LoadingStateEnum.Loading;
        public enum LoadingStateEnum
        {
            Loading = 0,
            Success = 1,
            Fail = 2,
            NoGeolocationAccess = 3
        }
        public List<MaintainPositionItem> MaintainPositionList { get; } = new List<MaintainPositionItem>();
        public event Action FetchMaintainPositionEnded;
        public void FetchMaintainPosition()
        {

            Task.Run(async () =>
            {
                var accessStatus = await Geolocator.RequestAccessAsync();
                if (accessStatus != GeolocationAccessStatus.Allowed)
                {
                    LoadingState = LoadingStateEnum.NoGeolocationAccess;
                    FetchMaintainPositionEnded?.Invoke();
                    FetchMaintainPositionEnded = null;
                    //MiniModeService.Instance.UpdateMiniUI(this);
                    return;
                }
                var geolocator = new Geolocator { DesiredAccuracyInMeters = 0 };
                var position = await geolocator.GetGeopositionAsync();
                try
                {
                    string data = await ServerApi.Instance.GetMaintainPosition(position.Coordinate.Point.Position.Latitude, position.Coordinate.Point.Position.Longitude).ConfigureAwait(false);
                    JsonObject jo = JsonObject.Parse(data);
                    if (jo["code"].GetNumber() == 200)
                    {
                        JsonArray ja = jo["data"].GetArray();
                        foreach (var item in ja)
                        {
                            JsonObject positionObject = item.GetObject();
                            MaintainPositionItem maintainPositionItem = new MaintainPositionItem();
                            maintainPositionItem.Latitude = positionObject["latitude"].GetNumber();
                            maintainPositionItem.Longitude = positionObject["longitude"].GetNumber();
                            maintainPositionItem.Name = positionObject["name"].GetString().Replace("\r", "").Replace("\n", "");
                            maintainPositionItem.Addr = positionObject["addr"].GetString().Replace("\r", "").Replace("\n", "");
                            maintainPositionItem.Distance = (int)Helpers.CoordinateHelper.Distance(maintainPositionItem.Latitude, maintainPositionItem.Longitude
                                , position.Coordinate.Point.Position.Latitude, position.Coordinate.Point.Position.Longitude);
                            MaintainPositionList.Add(maintainPositionItem);
                        }
                        LoadingState = LoadingStateEnum.Success;
                    }
                    else
                    {
                        LoadingState = LoadingStateEnum.Fail;
                    }
                }
                catch
                {
                    LoadingState = LoadingStateEnum.Fail;
                }
                FetchMaintainPositionEnded?.Invoke();
                FetchMaintainPositionEnded = null;
                //MiniModeService.Instance.UpdateMiniUI(this);
            });
        }
    }
}
